home *** CD-ROM | disk | FTP | other *** search
/ Aminet 33 / Aminet 33 - October 1999.iso / Aminet / gfx / show / vmpeg.lha / src / getbits.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-07-06  |  5.3 KB  |  246 lines

  1. /* getbits.c, bit level routines                                            */
  2.  
  3. /*
  4.  * All modifications (mpeg2decode -> mpeg2play) are
  5.  * Copyright (C) 1996, Stefan Eckart. All Rights Reserved.
  6.  */
  7.  
  8. /* Copyright (C) 1996, MPEG Software Simulation Group. All Rights Reserved. */
  9.  
  10. /*
  11.  * Disclaimer of Warranty
  12.  *
  13.  * These software programs are available to the user without any license fee or
  14.  * royalty on an "as is" basis.  The MPEG Software Simulation Group disclaims
  15.  * any and all warranties, whether express, implied, or statuary, including any
  16.  * implied warranties or merchantability or of fitness for a particular
  17.  * purpose.  In no event shall the copyright-holder be liable for any
  18.  * incidental, punitive, or consequential damages of any kind whatsoever
  19.  * arising from the use of these programs.
  20.  *
  21.  * This disclaimer of warranty extends to the user of these programs and user's
  22.  * customers, employees, agents, transferees, successors, and assigns.
  23.  *
  24.  * The MPEG Software Simulation Group does not represent or warrant that the
  25.  * programs furnished hereunder are free of infringement of any third-party
  26.  * patents.
  27.  *
  28.  * Commercial implementations of MPEG-1 and MPEG-2 video, including shareware,
  29.  * are subject to royalty fees to patent holders.  Many of these patents are
  30.  * general enough such that they are unavoidable regardless of implementation
  31.  * design.
  32.  *
  33.  */
  34.  
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37.  
  38. #include "config.h"
  39. #include "global.h"
  40.  
  41.  
  42.  
  43. void Initialize_Buffer()
  44. /* initialize buffer, call once before first getbits or showbits */
  45. {
  46.   ld->Incnt = 0;
  47.   ld->Rdptr = ld->Rdbfr + STREAMBUFSIZE;
  48.   ld->Rdmax = ld->Rdptr;
  49.  
  50. #ifdef VERIFY
  51.   /*  only the verifier uses this particular bit counter 
  52.    *  Bitcnt keeps track of the current parser position with respect
  53.    *  to the video elementary stream being decoded, regardless 
  54.    *  of whether or not it is wrapped within a systems layer stream 
  55.    */
  56.   ld->Bitcnt = 0;
  57. #endif
  58.  
  59.   ld->Bfr = 0;
  60.   Flush_Buffer(0); /* fills valid data into bfr */
  61. }
  62.  
  63.  
  64. void Fill_Buffer()
  65. {
  66.   int Buffer_Level;
  67.  
  68.   Buffer_Level = fread(ld->Rdbfr,1,STREAMBUFSIZE,ld->Infile);
  69.   ld->Rdptr = ld->Rdbfr;
  70.  
  71.   if (System_Stream_Flag)
  72.     ld->Rdmax -= STREAMBUFSIZE;
  73.  
  74.   
  75.   /* end of the bitstream file */
  76.   if (Buffer_Level < STREAMBUFSIZE)
  77.   {
  78.     /* just to be safe */
  79.     if (Buffer_Level < 0)
  80.       Buffer_Level = 0;
  81.  
  82.     /* pad until the next to the next 32-bit word boundary */
  83.     while (Buffer_Level & 3)
  84.       ld->Rdbfr[Buffer_Level++] = 0;
  85.  
  86.   /* pad the buffer with sequence end codes */
  87.     while (Buffer_Level < STREAMBUFSIZE)
  88.     {
  89.       ld->Rdbfr[Buffer_Level++] = SEQUENCE_END_CODE>>24;
  90.       ld->Rdbfr[Buffer_Level++] = SEQUENCE_END_CODE>>16;
  91.       ld->Rdbfr[Buffer_Level++] = SEQUENCE_END_CODE>>8;
  92.       ld->Rdbfr[Buffer_Level++] = SEQUENCE_END_CODE&0xff;
  93.     }
  94.   }
  95. }
  96.  
  97.  
  98. #ifndef __PPC__
  99. int Get_Byte()
  100. /* MPEG-1 system layer demultiplexer */
  101. {
  102.   while(ld->Rdptr >= ld->Rdbfr+STREAMBUFSIZE)
  103.   {
  104.     fread(ld->Rdbfr,1,STREAMBUFSIZE,ld->Infile);
  105.     ld->Rdptr -= STREAMBUFSIZE;
  106.     ld->Rdmax -= STREAMBUFSIZE;
  107.   }
  108.   return *ld->Rdptr++;
  109. }
  110. #endif /* __PPC__ */
  111.  
  112.  
  113. #ifndef __PPC__
  114. int Get_Word()
  115. /* extract a 16-bit word from the bitstream buffer */
  116. {
  117.   int Val;
  118.  
  119.   Val = Get_Byte();
  120.   return (Val<<8) | Get_Byte();
  121. }
  122. #endif /* __PPC__ */
  123.  
  124.  
  125. #ifndef __PPC__
  126. unsigned int Show_Bits(int N)
  127. /* return next n bits (right adjusted) without advancing */
  128. {
  129.   return ld->Bfr >> (32-N);
  130. }
  131. #endif /* __PPC__ */
  132.  
  133.  
  134. #ifdef __PPC__
  135. unsigned int Get_Bits1()
  136. /* return next bit (could be made faster than Get_Bits(1)) */
  137. {
  138.   unsigned int Val;
  139.  
  140.   Val = Show_Bits(1);
  141.   Flush_Buffer(1);
  142.  
  143.   return Val;
  144. }
  145. #endif /* __PPC__ */
  146.  
  147.  
  148. #ifdef __PPC__
  149. void _Flush_Buffer(int Incnt)
  150. {
  151.   if (System_Stream_Flag && (ld->Rdptr >= ld->Rdmax-4)) {
  152.     do {
  153.       if (ld->Rdptr >= ld->Rdmax)
  154.         Next_Packet();
  155.       ld->Bfr |= Get_Byte() << (24 - Incnt);
  156.       Incnt += 8;
  157.     }
  158.     while (Incnt <= 24);
  159.   }
  160.   else if (ld->Rdptr < ld->Rdbfr+(STREAMBUFSIZE-4)) {
  161.     do {
  162.       ld->Bfr |= *ld->Rdptr++ << (24 - Incnt);
  163.       Incnt += 8;
  164.     }
  165.     while (Incnt <= 24);
  166.   }
  167.   else {
  168.     do {
  169.       if (ld->Rdptr >= ld->Rdbfr+STREAMBUFSIZE)
  170.         Fill_Buffer();
  171.       ld->Bfr |= *ld->Rdptr++ << (24 - Incnt);
  172.       Incnt += 8;
  173.     }
  174.     while (Incnt <= 24);
  175.   }
  176.   ld->Incnt = Incnt;
  177. }
  178.  
  179.  
  180. #else /* !__PPC__ */
  181. void Flush_Buffer(int N)
  182. /* advance by n bits */
  183. {
  184.   int Incnt;
  185.  
  186.   ld->Bfr <<= N;
  187.  
  188.   Incnt = ld->Incnt -= N;
  189.  
  190.   if (Incnt <= 24)
  191.   {
  192.     if (System_Stream_Flag && (ld->Rdptr >= ld->Rdmax-4))
  193.     {
  194.       do
  195.       {
  196.         if (ld->Rdptr >= ld->Rdmax)
  197.           Next_Packet();
  198.         ld->Bfr |= Get_Byte() << (24 - Incnt);
  199.         Incnt += 8;
  200.       }
  201.       while (Incnt <= 24);
  202.     }
  203.     else if (ld->Rdptr < ld->Rdbfr+(STREAMBUFSIZE-4))
  204.     {
  205.       do
  206.       {
  207.         ld->Bfr |= *ld->Rdptr++ << (24 - Incnt);
  208.         Incnt += 8;
  209.       }
  210.       while (Incnt <= 24);
  211.     }
  212.     else
  213.     {
  214.       do
  215.       {
  216.         if (ld->Rdptr >= ld->Rdbfr+STREAMBUFSIZE)
  217.           Fill_Buffer();
  218.         ld->Bfr |= *ld->Rdptr++ << (24 - Incnt);
  219.         Incnt += 8;
  220.       }
  221.       while (Incnt <= 24);
  222.     }
  223.     ld->Incnt = Incnt;
  224.   }
  225.  
  226. #ifdef VERIFY 
  227.   ld->Bitcnt += N;
  228. #endif /* VERIFY */
  229.  
  230. }
  231. #endif /* __PPC__ */
  232.  
  233.  
  234. #ifdef __PPC__
  235. unsigned int Get_Bits(int N)
  236. /* return next n bits (right adjusted) */
  237. {
  238.   unsigned int Val;
  239.  
  240.   Val = Show_Bits(N);
  241.   Flush_Buffer(N);
  242.  
  243.   return Val;
  244. }
  245. #endif /* __PPC__ */
  246.